// pages/upload/index.js
const app = getApp();
const getRecipeUrl = require("../../config.js").medicineRecipeUrl
const getBPRecordUrl = require("../../config.js").bloodPressDailyCountUrl
Page({

  /**
   * 页面的初始数据
   */
  data: {
    BDRecordCount: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("has_bound")
    var has_bound = wx.getStorageSync('has_bound');
    console.log(has_bound == null || !has_bound)
    if (has_bound == null || !has_bound) {
      wx.redirectTo({
        url: '../card-id/index',
      })
    }else{
      app.hasBoundCallback=data=>{
        wx.setStorageSync('token', data.token);
        wx.setStorageSync('has_bound', data.boundCardNum);
      }
    }
    var token = wx.getStorageSync('token');
    wx.request({
      url: getRecipeUrl,
      header: {
        'Authorization': "Token " + token
      },
      method: "GET",
      success: function(res){
        console.log(res.data)
      }
    })
    var that = this;
    wx.request({
      url: getBPRecordUrl,
      header: {
        'Authorization': "Token " + token
      },
      method: 'GET',
      success: function (res) {
        console.log(res.data.dailyCount)
        that.setData({ BDRecordCount: res.data.dailyCount})
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var token = wx.getStorageSync('token');
    var that = this;
    wx.request({
      url: getBPRecordUrl,
      header: {
        'Authorization': "Token " + token
      },
      method: 'GET',
      success: function (res) {
        console.log(res.data.dailyCount)
        that.setData({ BDRecordCount: res.data.dailyCount })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var token = wx.getStorageSync('token');
    var that = this;
    wx.request({
      url: getBPRecordUrl,
      header: {
        'Authorization': "Token " + token
      },
      method: 'GET',
      success: function (res) {
        console.log(res.data.dailyCount)
        that.setData({ BDRecordCount: res.data.dailyCount })
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  nav_blood_upload:function(){
    wx.navigateTo({
      url: '../index/index',
    })
  },
  nav_medicine_upload:function(){
    wx.navigateTo({
      url: '../medicine/medicine',
    })
  }
})